
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>资讯分类列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <button class="layui-btn layuiadmin-btn-tags" data-type="add">添加</button>
        </div>
        <div class="layui-card-body">
            <table id="table" lay-filter="table"></table>
            <script type="text/html" id="buttonTpl">
                {{#  if(d.acc_status == 1){ }}
                <button class="layui-btn layui-btn-xs">已启用</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">已禁用</button>
                {{#  } }}
            </script>
            <script type="text/html" id="layuiadmin-app-cont-tagsbar">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                {{# if(d.acc_parent_id== -1){ }}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="addChildCategory"><i class="layui-icon layui-icon-add-1"></i>添加子分类</a>
                {{# } }}
            </script>
            <script type="text/html" id="acc_name">
                {{# if(d.acc_parent_id==-1){   }}
                <p>{{d.acc_name}}</p>
                {{#  }else{  }}
                <p style="display: inline-block;text-align: left;width: 158px;float: right;">—— {{d.acc_name}}</p>
                {{# } }}
                </script>
        </div>
    </div>
</div>

<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function(){
        var table=layui.table;

        //分类管理
        table.render({
            elem: '#table'
            ,url: "{:url('admin/article/articleCategoryList')}"
            ,cols: [[
                {field: 'index', width: 100, title: 'ID', sort: true,align:"center"}
                ,{field: 'acc_name', title: '类目名称', minWidth: 100,align:"center",templet:"#acc_name"}
                ,{field: 'acc_sort', title: '类目排序', minWidth: 100,align:"center"}
                , {field: 'acc_status', title: '状态', templet: '#buttonTpl', minWidth: 80, align: 'center'}
                ,{title: '操作', width:200, align: 'left', fixed: 'right', toolbar: '#layuiadmin-app-cont-tagsbar'}
            ]]
            ,text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
        });

        var $ = layui.$
        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加类目'
                    ,content:"{:url ('admin/article/articleCategoryAdd')}"
                    ,area: ['800px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){

                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'category-add'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交的字段
                            // acc_name: "123"
                            // acc_order: "123"
                            // acc_status: "on"
                            // acc_thumb: "\uploads\admin\20190912\cd989ee0c439507969a2be1056b8e3b1.gif"
                            // acc_url: "123"
                            $.ajax({
                                url: "{:url ('admin/article/articleCategoryAdd')}",
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        //当处理成功时，先关闭弹窗，再提示信息
                                        layer.close(index);
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                            // //只有在添加成功时，才重新加载表格并关闭弹窗
                                            table.reload('table');
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                            return false;
                        });

                        //触发提交
                        submit.trigger('click');
                    }
                });
            }
        }
        //监听工具条
        table.on('tool(table)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除此平台？', function(index){
                    // data={tac_id: 1, tac_name: "淘宝"}
                    $.ajax({
                        url: "{:url ('admin/task/taskClassDel')}?XDEBUG_SESSION_START=11682&tac_id="+data.tac_id,
                        type: "post",
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1) {
                                layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                    table.reload('table');
                                    layer.close(index);
                                });
                            } else {
                                layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                    layer.close(index);
                                });
                            }
                        }
                    })



                });
            }
            else if(obj.event === 'edit'){
                var acc_id=data.acc_id;
                layer.open({
                    type: 2
                    ,title: '编辑类目'
                    ,content: "{:url ('admin/article/articleCategoryEdit')}?acc_id="+acc_id
                    ,area: ['800px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'category-edit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            $.ajax({
                                url: "{:url ('admin/article/articleCategoryEdit')}?acc_id="+acc_id,
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        layer.close(index);
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                            table.reload('table');
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                            return false;
                        });
                        submit.trigger('click');

                    }
                    ,success: function(layero, index){
                    }
                });
            }
            else if(obj.event==='addChildCategory'){
                const data = obj.data;
                const acc_id=data.acc_id;
                layer.open({
                    type: 2
                    ,title: '添加子类目'
                    ,content:"{:url ('admin/article/articleCategoryAdd')}?acc_parent_id="+acc_id
                    ,area: ['800px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'category-add'
                            , submit = layero.find('iframe').contents().find('#' + submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交的字段
                            // acc_name: "123"
                            // acc_order: "123"
                            // acc_status: "on"
                            // acc_thumb: "\uploads\admin\20190912\cd989ee0c439507969a2be1056b8e3b1.gif"
                            // acc_url: "123"
                            $.ajax({
                                url: "{:url ('admin/article/articleCategoryAdd')}?acc_parent_id="+acc_id,
                                type: "post",
                                data: field,
                                dataType: "json",
                                success: function (res) {
                                    if (res.code == 1) {
                                        //当处理成功时，先关闭弹窗，再提示信息
                                        layer.close(index);
                                        layer.msg(res.msg, {icon: 1, time: 1500}, function () {
                                            // //只有在添加成功时，才重新加载表格并关闭弹窗
                                            table.reload('table');
                                        });
                                    } else {
                                        layer.msg(res.msg, {icon: 5, time: 1500}, function () {
                                        });
                                    }
                                }
                            })
                            return false;
                        });

                        //触发提交
                        submit.trigger('click');
                    }
                });

            }
        });

        $('.layui-btn.layuiadmin-btn-tags').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
